package com.example.service.impl;

import com.example.dao.SalaryDao;
import com.example.domain.*;
import com.example.service.SalaryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author luo
 */
@Service
public class SalaryServiceImpl implements SalaryService {

    @Autowired
    private SalaryDao salaryDao;


    @Override
    public List<Salary> selectSalary() {
        return salaryDao.selectSalary();
    }

    @Override
    public Salary selectSalaryById(Integer id) {
        return salaryDao.selectSalaryById(id);
    }

    @Override
    public List<Salary> selectSalaryByName(String name) {
        return salaryDao.selectSalaryByName(name);
    }

    @Override
    public int addSalary(Salary salary) {
        return salaryDao.addSalary(salary);
    }

    @Override
    public int updateSalary(Salary salary) {
        return salaryDao.updateSalary(salary);
    }

    @Override
    public int deleteSalary(Integer id) {
        return salaryDao.deleteSalary(id);
    }

    @Override
    public PageInfo<Salary> findPage(int page, int size) {
        // 开启分页插件,放在查询语句上面 帮助生成分页语句
        PageHelper.startPage(page, size); //底层实现原理采用改写语句   将下面的方法中的sql语句获取到然后做个拼接 limit AOP技术
        List<Salary> listUser = salaryDao.selectSalary();
        // 封装分页之后的数据  返回给客户端展示  PageInfo做了一些封装 作为一个类
        PageInfo<Salary> pageInfoUser = new PageInfo<Salary>(listUser);
        return pageInfoUser;
    }
    /**
     * 结果的导出
     */
    @Override
    public ExcelData getOutputExcelProjectData() {


        //Excel实体类
        ExcelData excelData = new ExcelData();

        //找到表中需要添加的数据
        List<Salary> list = salaryDao.selectSalary();

        //设置sheet名称
        excelData.setName("用户信息");

        //设置表头字段
        List<String> titles = new ArrayList<String>();
        titles.add("Id");
        titles.add("员工姓名");
        titles.add("工资");
        titles.add("全勤薪资");
        titles.add("培训薪资");
        titles.add("迟到薪资");
        titles.add("旷工薪资");
        titles.add("保险薪资");
        titles.add("总工资(含5%税)");
        excelData.setTitles(titles);

        //把expertUserList转换为List<List<Object>> rows
        List<List<Object>> rows = new ArrayList<List<Object>>();

        //加入数据
        for (Salary salary : list) {
            List<Object> row = new ArrayList<Object>();
            row.add(salary.getId());
            row.add(salary.getName());
            row.add(salary.getWages());
            row.add(salary.getAllday());
            row.add(salary.getTrain());
            row.add(salary.getOvertime());
            row.add(salary.getAbsent());
            row.add(salary.getInsurance());
            row.add(salary.getSum());
            rows.add(row);
        }

        //设置要输出的记录
        excelData.setRows(rows);
        return excelData;
    }
}
